package com.hc.pingtan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.hc.pingtan.mapper.KekeMerchantsMapper;
import com.hc.pingtan.mapper.KekeWitkeyIndustryMapper;
import com.hc.pingtan.mapper.KekeWitkeyTaskMapper;
import com.hc.pingtan.model.CreateTask;
import com.hc.pingtan.model.GetTaskDetail;
import com.hc.pingtan.model.KekeMerchants;
import com.hc.pingtan.model.KekeWitkeyIndustry;
import com.hc.pingtan.model.KekeWitkeyTask;
import com.hc.pingtan.model.KekeWitkeyTaskDetail;
import com.hc.pingtan.model.KekeWitkeyTaskQuery;
import com.hc.pingtan.model.PageBean;
import com.hc.pingtan.model.QueryIndustry;
import com.hc.pingtan.model.UpdateTaskStatus;
import com.hc.pingtan.service.IKekeWitkeyTaskService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-10-25
 */
@Service
@DS("db1")
public class KekeWitkeyTaskServiceImpl extends ServiceImpl<KekeWitkeyTaskMapper, KekeWitkeyTask> implements IKekeWitkeyTaskService {
    @Resource
    private KekeMerchantsMapper kekeMerchantsMapper;
    @Resource
    private KekeWitkeyIndustryMapper kekeWitkeyIndustryMapper;

    @Override
    public PageInfo<KekeWitkeyTask> pageList(KekeWitkeyTaskQuery kekeWitkeyTaskQuery) {
        PageBean pageBean = kekeWitkeyTaskQuery.getPageBean();
        PageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize());
        List<KekeWitkeyTask> listAll = baseMapper.findListAll(kekeWitkeyTaskQuery);
        return new PageInfo<>(listAll);
    }

    @Override
    public void createTask(CreateTask createTask) {
        KekeWitkeyTask kekeWitkeyTask = new KekeWitkeyTask();
        BeanUtil.copyProperties(createTask, kekeWitkeyTask);
        kekeWitkeyTask.setUniqueNum(DateUtil.format(new Date(), "YYYYMMddHHmmss"));
        kekeWitkeyTask.setModelId("1");
        kekeWitkeyTask.setUid(1);
        kekeWitkeyTask.setUsername("admin");
        kekeWitkeyTask.setStartTime(Integer.parseInt(System.currentTimeMillis() / 1000 + ""));
        kekeWitkeyTask.setTaskStatus(1);
        baseMapper.insert(kekeWitkeyTask);
    }

    @Override
    public KekeWitkeyTaskDetail getTaskDetail(GetTaskDetail getTaskDetail) {
        KekeWitkeyTask kekeWitkeyTask = baseMapper.selectById(getTaskDetail.getTaskId());

        QueryWrapper<KekeMerchants> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("task_Id", kekeWitkeyTask.getTaskId());
        List<KekeMerchants> kekeMerchants = kekeMerchantsMapper.selectList(queryWrapper);

        KekeWitkeyTaskDetail kekeWitkeyTaskDetail = new KekeWitkeyTaskDetail();
        BeanUtil.copyProperties(kekeWitkeyTask, kekeWitkeyTaskDetail);
        kekeWitkeyTaskDetail.setKekeMerchantsList(kekeMerchants);
        return kekeWitkeyTaskDetail;
    }

    @Override
    public void updateTaskStatus(UpdateTaskStatus updateTaskStatus) {
        KekeWitkeyTask kekeWitkeyTask = baseMapper.selectById(updateTaskStatus.getTaskId());
        kekeWitkeyTask.setTaskStatus(updateTaskStatus.getTaskStatus());
        baseMapper.updateById(kekeWitkeyTask);
    }

    @Override
    public Collection<QueryIndustry> queryIndustry() {
        QueryWrapper<KekeWitkeyIndustry> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("indus_pid", 0);
        List<KekeWitkeyIndustry> kekeWitkeyIndustries = kekeWitkeyIndustryMapper.selectList(queryWrapper);
        Map<Integer, KekeWitkeyIndustry> kekeWitkeyIndustryMap = kekeWitkeyIndustries.stream().collect(Collectors.toMap(KekeWitkeyIndustry::getIndusId,
                e -> e));

        QueryWrapper<KekeWitkeyTask> kekeWitkeyTaskQuery = new QueryWrapper<>();
        kekeWitkeyTaskQuery.in("model_id", Lists.newArrayList(1, 2, 3, 4, 5, 12));
        kekeWitkeyTaskQuery.notIn("task_status", Lists.newArrayList(0, 1, 10));

        Map<Integer, QueryIndustry> kekeWitkeyTaskMap = new HashMap<>();
        List<KekeWitkeyTask> kekeWitkeyTasks = baseMapper.selectList(kekeWitkeyTaskQuery);
        for (KekeWitkeyTask kekeWitkeyTask : kekeWitkeyTasks) {
            if (!kekeWitkeyTaskMap.containsKey(kekeWitkeyTask.getIndusPid()) && null != kekeWitkeyIndustryMap.get(kekeWitkeyTask.getIndusPid())
            && StringUtils.isNotBlank(kekeWitkeyIndustryMap.get(kekeWitkeyTask.getIndusPid()).getIndusName())) {
                QueryIndustry queryIndustry = QueryIndustry.builder()
                        .kekeWitkeyIndustry(kekeWitkeyIndustryMap.get(kekeWitkeyTask.getIndusPid()))
                        .taskList(Lists.newArrayList())
                        .build();
                kekeWitkeyTaskMap.put(kekeWitkeyTask.getIndusPid(), queryIndustry);
            }

            QueryIndustry queryIndustry = kekeWitkeyTaskMap.get(kekeWitkeyTask.getIndusPid());
            if(queryIndustry != null){
                queryIndustry.getTaskList().add(kekeWitkeyTask);
            }
        }
        return kekeWitkeyTaskMap.values();
    }
}
